import Http from '@/utils/http'
import Qs from 'qs'

class Common {
  request (http) {
    return new Promise((resolve, reject) => {
      http.then(response => {
        if (response.status === 200 || response.status === 204) {
          resolve(response.data)
        }
        reject(response)
      })
        .catch(error => {
          reject(error)
        })
    })
  }

  /**
   * get获取数据
   *
   * @returns {*}
   */
  get (url, params = {}) {
    return this.request(Http.get(url, {params: params}))
  }

  post (url, params = {}) {
    console.log('post', params)
    let postData = Qs.stringify(params)
    return this.request(Http.post(url, postData, {
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
      }
    }))
  }

  /**
   * put修改数据
   *
   * @param url
   * @param params
   * @returns {*}
   */
  put (url, params = {}) {
    return this.request(Http.put(url, params), {
      headers: {
        'Content-Type': 'application/json'
      }
    })
  }
}

export default Common
